package com.gap.revision;

import com.buanalintas.framework.Action;
import com.buanalintas.framework.Controller;
import com.buanalintas.framework.Dictionary;
import com.gap.entities.GapTransactionRevision;
import com.buanalintas.framework.Field;
import com.buanalintas.framework.Logger;
import com.buanalintas.framework.Page;
import com.buanalintas.framework.Persistence;
import com.buanalintas.framework.Sequence;
import com.buanalintas.framework.Utility;
import com.buanalintas.framework.formatter.DateFormatter;
import com.gap.employee.Employee;
import com.gap.entities.GapMasterEmployee;
import com.gap.entities.GapMasterHistoryEmployee;
import com.gap.entities.GapTransactionEmployee;
import com.gap.entities.GapTransactionHistoryEmployee;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * Class Revision is ...
 *
 * Generated on Fri Jan 09 13:17:48 ICT 2015
 *
 * @version 1.0
 * @author  Mr. Kazao™
 * @since   Bynga 1.0
 *
 */
public class Revision extends Page {

    
    private Field revisionId;
    
    private Field revisionEmployeeName;
    
    private Field revisionDate;
    
    private Field revisionItem;
    
    private Field revisionItemPrice;
    
    private Field revisionDiscountNominal;
    
    private Field revisionLast;
    
    private Field revisionCurrent;
    
    private Field revisionValue;
    
    private Field revisionPlatform;
    
    private Field revisionNumber;
    
    private Field revisionKnownBy;
    
    private Field revisionDescription;
    
    private Field revisionTimestamp;
    
    private Field revisionPosId;
    
    private Field revisionEmployeeId;
    
    private Field revisionUnitId;
    
    private Field revisionUserId;
    

    public Revision() {
        super();

        setTheClass(GapTransactionRevision.class);
        
        
        
        
        revisionId = new Field();
        revisionId.setName("revisionId");
        revisionId.setCaption("revision id");
        //revisionId.setInfo("revision id");
        revisionId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionId.setInputProperties(" size=\"20\"");        
        getFields().add(revisionId);
        
        revisionEmployeeName = new Field();
        revisionEmployeeName.setName("revisionEmployeeName");
        revisionEmployeeName.setCaption("employee name");
        revisionEmployeeName.setRequired(true);
        revisionEmployeeName.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionEmployeeName.setInputProperties(" size=\"20\"");        
        getFields().add(revisionEmployeeName);
        
        revisionItem = new Field();
        revisionItem.setName("revisionItem");
        revisionItem.setCaption("item");
        revisionItem.setRequired(true);
        revisionItem.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionItem.setInputProperties(" size=\"20\"");        
        getFields().add(revisionItem);
        
        revisionDate = new Field();
        revisionDate.setName("revisionDate");
        revisionDate.setCaption("date");
        revisionDate.setRequired(true);
        revisionDate.setListProperties(" align=\"center\" width=\"1%\" nowrap");
        revisionDate.setInputProperties(" size=\"8\"");
        revisionDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(revisionDate);
        
        revisionItemPrice = new Field();
        revisionItemPrice.setName("revisionItemPrice");
        revisionItemPrice.setCaption("last");
        //revisionItemPrice.setInfo("revision last");
        revisionItemPrice.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionItemPrice.setInputProperties(" size=\"20\"");        
        getFields().add(revisionItemPrice);
        
        revisionDiscountNominal = new Field();
        revisionDiscountNominal.setName("revisionDiscountNominal");
        revisionDiscountNominal.setCaption("discount ");
        //revisionItemPrice.setInfo("revision last");
        revisionDiscountNominal.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        revisionDiscountNominal.setInputProperties(" size=\"20\"");        
        getFields().add(revisionDiscountNominal);
        
        revisionLast = new Field();
        revisionLast.setName("revisionLast");
        revisionLast.setCaption("last");
        //revisionLast.setInfo("revision last");
        revisionLast.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionLast.setInputProperties(" size=\"20\"");        
        getFields().add(revisionLast);
        
        revisionCurrent = new Field();
        revisionCurrent.setName("revisionCurrent");
        revisionCurrent.setCaption("saldo");
        //revisionCurrent.setInfo("revision current");
        revisionCurrent.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        revisionCurrent.setInputProperties(" size=\"20\"");        
        getFields().add(revisionCurrent);
        
        revisionValue = new Field();
        revisionValue.setName("revisionValue");
        revisionValue.setCaption("value");
        revisionValue.setRequired(true);
        revisionValue.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        revisionValue.setInputProperties(" size=\"20\" readonly");        
        getFields().add(revisionValue);
        
        revisionPlatform = new Field();
        revisionPlatform.setName("revisionPlatform");
        revisionPlatform.setCaption("platform");
        revisionPlatform.setRequired(true);
        revisionPlatform.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        revisionPlatform.setInputProperties(" size=\"20\" readonly");        
        getFields().add(revisionPlatform);
        
        revisionNumber = new Field();
        revisionNumber.setName("revisionNumber");
        revisionNumber.setCaption("number");
        revisionNumber.setRequired(true);
        revisionNumber.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        revisionNumber.setInputProperties(" size=\"50\" readonly");        
        getFields().add(revisionNumber);
        
        revisionKnownBy = new Field();
        revisionKnownBy.setName("revisionKnownBy");
        revisionKnownBy.setCaption("known by");
        //revisionKnownBy.setInfo("revision known by");
        revisionKnownBy.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        revisionKnownBy.setInputProperties(" size=\"30\"");        
        getFields().add(revisionKnownBy);
        
        revisionDescription = new Field();
        revisionDescription.setName("revisionDescription");
        revisionDescription.setCaption("description");
        //revisionDescription.setInfo("revision description");
        revisionDescription.setListProperties(" width=\"20%\"");
        revisionDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        revisionDescription.setInputType(Field.INPUT_TEXT_AREA);        
        getFields().add(revisionDescription);
        
        revisionTimestamp = new Field();
        revisionTimestamp.setName("revisionTimestamp");
        revisionTimestamp.setCaption("timestamp");
        //revisionTimestamp.setInfo("revision timestamp");
        revisionTimestamp.setListProperties(" align=\"center\" width=\"1%\" nowrap");
        revisionTimestamp.setInputProperties(" size=\"12\"");
        //revisionTimestamp.setFormatter(DateFormatter.getInstanceDateTime(this));
        getFields().add(revisionTimestamp);
        
        revisionPosId = new Field();
        revisionPosId.setName("revisionPosId");
        revisionPosId.setCaption("transaction");
        revisionPosId.setRequired(true);
        revisionPosId.setSelectorOrder("t.posTimestamp DESC");
        revisionPosId.getSelectorAction().add(new String[]{"value", "revisionEmployeeName", "posEmployeeName"});
        revisionPosId.getSelectorAction().add(new String[]{"value", "revisionPlatform", "posPlatform"});
        revisionPosId.getSelectorAction().add(new String[]{"value", "revisionItem", "posItem"});
        revisionPosId.getSelectorAction().add(new String[]{"value", "revisionItemPrice", "posItemPrice"});
        revisionPosId.getSelectorAction().add(new String[]{"value", "revisionDiscountNominal", "posDiscountNominal"});
        revisionPosId.getSelectorAction().add(new String[]{"value", "revisionValue", "posValue"});
        revisionPosId.getSelectorColumns().add(new String[]{"posNumber", "Number", "width=\"20%\" nowrap"});
        revisionPosId.getSelectorColumns().add(new String[]{"posEmployeeId", "ID Employee", "width=\"10%\""});
        revisionPosId.getSelectorColumns().add(new String[]{"posEmployeeName", "Name", "width=\"30%\""});
        revisionPosId.getSelectorColumns().add(new String[]{"posPlatform", "Platform", "width=\"10%\" nowrap"}); 
        revisionPosId.getSelectorColumns().add(new String[]{"posCurrent", "Current", "width=\"10%\" nowrap"}); 
        revisionPosId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        revisionPosId.setInputProperties(" size=\"30\"");        
        getFields().add(revisionPosId);
        
        revisionEmployeeId = new Field();
        revisionEmployeeId.setName("revisionEmployeeId");
        revisionEmployeeId.setCaption("employee");
        //revisionPosId.setInfo("revision pos id");
        revisionEmployeeId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionEmployeeId.setInputProperties(" size=\"20\"");        
        getFields().add(revisionEmployeeId);
        
        revisionUnitId = new Field();
        revisionUnitId.setName("revisionUnitId");
        revisionUnitId.setCaption("unit");
        //revisionUnitId.setInfo("revision unit id");
        revisionUnitId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionUnitId.setInputProperties(" size=\"20\"");        
        getFields().add(revisionUnitId);
        
        revisionUserId = new Field();
        revisionUserId.setName("revisionUserId");
        revisionUserId.setCaption("user");
        //revisionUserId.setInfo("revision user id");
        revisionUserId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        revisionUserId.setInputProperties(" size=\"20\"");        
        getFields().add(revisionUserId);
        

        getActions().add(Action.TRANS_ADD);
        getActions().add(Action.TRANS_EDIT);
        getActions().add(Action.TRANS_VIEW);
        fillFields();
        getFieldsAdd().clear();
        getFieldsAdd().clear();
        getFieldsAdd().add(revisionDate);
        getFieldsAdd().add(revisionNumber);
        getFieldsAdd().add(revisionKnownBy);
        getFieldsAdd().add(revisionPosId);
        getFieldsAdd().add(revisionEmployeeName);
        getFieldsAdd().add(revisionPlatform);
        getFieldsAdd().add(revisionItem);
        getFieldsAdd().add(revisionItemPrice);
        getFieldsAdd().add(revisionDiscountNominal);
        getFieldsAdd().add(revisionValue);
        getFieldsAdd().add(revisionDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(revisionDate);
        getFieldsEdit().add(revisionNumber);
        getFieldsEdit().add(revisionKnownBy);
        getFieldsEdit().add(revisionPosId);
        getFieldsEdit().add(revisionEmployeeName);
        getFieldsEdit().add(revisionPlatform);
        getFieldsEdit().add(revisionItem);
        getFieldsEdit().add(revisionItemPrice);
        getFieldsEdit().add(revisionDiscountNominal);
        getFieldsEdit().add(revisionValue);
        getFieldsEdit().add(revisionDescription);

        getFieldsList().clear();
        getFieldsList().add(revisionDate);
        getFieldsList().add(revisionNumber);
        getFieldsList().add(revisionKnownBy);
        getFieldsList().add(revisionPosId);
        getFieldsList().add(revisionEmployeeName);
        getFieldsList().add(revisionPlatform);
        getFieldsList().add(revisionItem);
        getFieldsList().add(revisionItemPrice);
        getFieldsList().add(revisionDiscountNominal);
        getFieldsList().add(revisionValue);
        getFieldsList().add(revisionDescription);

        getFieldsView().clear();
        getFieldsView().add(revisionDate);
        getFieldsView().add(revisionNumber);
        getFieldsView().add(revisionKnownBy);
        getFieldsView().add(revisionPosId);
        getFieldsView().add(revisionEmployeeId);
        getFieldsView().add(revisionEmployeeName);
        getFieldsView().add(revisionPlatform);
        getFieldsView().add(revisionItem);
        getFieldsView().add(revisionItemPrice);
        getFieldsView().add(revisionDiscountNominal);
        getFieldsView().add(revisionValue);
        getFieldsView().add(revisionDescription);
        getFieldsView().add(revisionUserId);
        getFieldsView().add(revisionUnitId);
        getFieldsView().add(revisionTimestamp);
        
        setController(new RevisionController(this));
        setExportList(getClass().getResourceAsStream("RevisionList.jasper"));
        setExport(getClass().getResourceAsStream("Revision.jasper"));
    }
    
    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        revisionDate.setFormatter(DateFormatter.getInstanceDate(this));
        revisionTimestamp.setFormatter(DateFormatter.getInstanceDateTime(this));

    }
    
     @Override
    public void beforeAdd() {
        revisionNumber.setDefaultValue(generate());
    }
    
    class RevisionController extends Controller {

        public RevisionController(Page page) {
            super(page);
            setOrder(revisionTimestamp);
            setOrderType("DESC");
        }

        @Override
        public void beforeInsert(Object data) {
            EntityManager em = getEntityManager();
            GapTransactionRevision revisi = (GapTransactionRevision) data;
            GapTransactionEmployee employ = revisi.getRevisionPosId();
            revisi.setRevisionNumber(generate());
            revisi.setRevisionUnitId(getCurrentUnit());
            revisi.setRevisionUserId(getCurrentUser());
            revisi.setRevisionTimestamp(Utility.getTimestamp());
            revisi.setRevisionEmployeeId(employ.getPosEmployeeId());
            revisi.getRevisionPosId().setPosDescription(""+ "Revision");

            GapMasterHistoryEmployee masterhis = Employee.getStock(em, revisi.getRevisionEmployeeId());
            GapTransactionHistoryEmployee history = new GapTransactionHistoryEmployee();

//            revisi.setRevisionLast(masterhis.getHistoryValue());

            BigDecimal Saldo = BigDecimal.ZERO;
            
            //--------EMPLOYEEE-----------
             GapMasterEmployee mase = null;
            try {
                mase = (GapMasterEmployee) em.createQuery("SELECT t FROM GapMasterEmployee t WHERE t.employeeId='" + revisi.getRevisionEmployeeId().getEmployeeId() + "'").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
                 System.out.println("mase jose ---->" + mase);
            } catch (Exception e) {
                Logger.log(e);
                System.out.println("eeeeee :"+e);
            }
            
            //-----ITEM------
            BigDecimal hargaTrans = BigDecimal.ZERO;
            BigDecimal hargaRev = BigDecimal.ZERO;
            BigDecimal nambahnya = BigDecimal.ZERO;
            
            hargaTrans = (employ.getPosItemPrice().subtract(employ.getPosDiscountNominal())).add(employ.getPosValue());
            hargaRev = (revisi.getRevisionItemPrice().subtract(revisi.getRevisionDiscountNominal())).add(revisi.getRevisionValue());
            
            int bandingkan = 0;
            bandingkan = hargaRev.compareTo(hargaTrans);
            //********JIKA Harga REVISI Lebih Besar***********
            //===============================================
            if(bandingkan == 1){
                nambahnya = hargaRev.subtract(hargaTrans);
                System.out.println("nambahnya 1---->" + nambahnya);
                
//                if (mase.getEmployeeStatusBonus() == "2") {
                Saldo = masterhis.getHistoryValue().add(nambahnya);
                System.out.println("Saldo 2 ---->" + Saldo);
                revisi.setRevisionCurrent(Saldo);
                
      //********JIKA Harga REVISI Lebih Kecil***********
            //===============================================
            }else if(bandingkan == -1){
                nambahnya = hargaTrans.subtract(hargaRev);
                System.out.println("nambahnya -1---->" + nambahnya);
                
                Saldo = masterhis.getHistoryValue().subtract(nambahnya);
                System.out.println("Saldo 2 ---->" + Saldo);
                revisi.setRevisionCurrent(Saldo);
                
            }else if(bandingkan == 0){
                nambahnya = hargaRev;
                System.out.println("nambahnya 0---->" + nambahnya);
            }          
           

            

        }

        @Override
        public void afterInsert(Object data) {
            EntityManager em = getEntityManager();
            em.getTransaction().begin();
            GapTransactionRevision revisi = (GapTransactionRevision) data;
            System.out.println("jose 1---->" + revisi);
            GapMasterHistoryEmployee masterhis = Employee.getStock(em, revisi.getRevisionEmployeeId());
            GapTransactionHistoryEmployee history = new GapTransactionHistoryEmployee();

            history.setHistoryId(Sequence.uuid());
            history.setHistoryEmployeeId(revisi.getRevisionEmployeeId());
            history.setHistoryUnitId(getCurrentUnit());
            history.setHistoryUserId(getCurrentUser());
            history.setHistoryDate(revisi.getRevisionDate());
            history.setHistoryStatus("1");
            history.setHistoryEmployeeName(revisi.getRevisionEmployeeName());
            history.setHistoryPlatform(revisi.getRevisionPlatform());
            history.setHistoryLast(masterhis.getHistoryValue());
            history.setHistoryCurrent(revisi.getRevisionCurrent());
            history.setHistoryValue(revisi.getRevisionValue());
            history.setHistoryDescription(revisi.getRevisionDescription());
            history.setHistoryTimestamp(revisi.getRevisionTimestamp());
            em.persist(history);
            masterhis.setHistoryValue(history.getHistoryCurrent());
            System.out.println("Curreennnttt ---->" + history.getHistoryCurrent());
            masterhis.setHistoryTimestamp(history.getHistoryTimestamp());
            masterhis.setHistoryUnitId(history.getHistoryUnitId());
            masterhis.setHistoryEmployeeId(history.getHistoryEmployeeId());
            masterhis.setHistoryEmployeeName(history.getHistoryEmployeeName());
            masterhis.setHistoryPlatform(history.getHistoryPlatform());
            

            em.getTransaction().commit();
        }
        
    }
    public String generate() {
        String unitCode = getCurrentUnit().getUnitCode();
        Calendar cal = Calendar.getInstance();
        String year = "" + cal.get(Calendar.YEAR);
        String month = Utility.intToRoman(cal.get(Calendar.MONTH) + 1);
        try {
            GapTransactionRevision employee = (GapTransactionRevision) getEntityManager().createQuery("SELECT t FROM GapTransactionRevision t WHERE t.revisionNumber LIKE '%/" + unitCode + "/GAP/%/" + year + "' ORDER BY t.revisionTimestamp DESC").setHint(Persistence.REFRESH_HINT, true).setFirstResult(0).setMaxResults(1).getSingleResult();
            return Utility.lpad(Integer.parseInt(employee.getRevisionNumber().substring(0, 4)) + 1, 4, "0") + "/" + unitCode + "/GAP/" + month + "/" + year;
        } catch (Exception e) {
        }
        return "0001/" + unitCode + "/Revision/" + month + "/" + year;
    }
    
     @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Transaction Revision"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }
}
